<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="接收平台" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.receivingPlatform`" :rules="formRules.receivingPlatform" class="mb-0px!">
            <el-input v-model="row.receivingPlatform" placeholder="请输入接收平台" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="类型" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.receivType`" :rules="formRules.receivType" class="mb-0px!">
            <el-input v-model="row.receivType" placeholder="请输入类型" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.name`" :rules="formRules.name" class="mb-0px!">
            <el-input v-model="row.name" placeholder="请输入名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="接收时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.receiveTime`" :rules="formRules.receiveTime" class="mb-0px!">
            <el-date-picker
              v-model="row.receiveTime"
              type="date"
              value-format="x"
              placeholder="选择接收时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="转发发布时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.forwardTime`" :rules="formRules.forwardTime" class="mb-0px!">
            <el-date-picker
              v-model="row.forwardTime"
              type="date"
              value-format="x"
              placeholder="选择转发发布时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="理论总数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.terminalTotal`" :rules="formRules.terminalTotal" class="mb-0px!">
            <el-input v-model="row.terminalTotal" placeholder="请输入理论总数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="应答总数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.ackTerminalTotal`" :rules="formRules.ackTerminalTotal" class="mb-0px!">
            <el-input v-model="row.ackTerminalTotal" placeholder="请输入应答总数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="等级响应数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.levelTotal`" :rules="formRules.levelTotal" class="mb-0px!">
            <el-input v-model="row.levelTotal" placeholder="请输入等级响应数量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="最早接收时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.earliestReceiveTime`" :rules="formRules.earliestReceiveTime" class="mb-0px!">
            <el-date-picker
              v-model="row.earliestReceiveTime"
              type="date"
              value-format="x"
              placeholder="选择最早接收时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="最晚接收时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.latestReceivingTime`" :rules="formRules.latestReceivingTime" class="mb-0px!">
            <el-date-picker
              v-model="row.latestReceivingTime"
              type="date"
              value-format="x"
              placeholder="选择最晚接收时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="平均接收时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.avgReceivingTime`" :rules="formRules.avgReceivingTime" class="mb-0px!">
            <el-date-picker
              v-model="row.avgReceivingTime"
              type="date"
              value-format="x"
              placeholder="选择平均接收时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加应答统计</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { Ack1Api } from '@/api/forward/ack'

const props = defineProps<{
  ack1Id: undefined // 所属应答（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.ack1Id,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await Ack1Api.getAck2ListByAck1Id(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    receivingPlatform: undefined,
    receivType: undefined,
    name: undefined,
    receiveTime: undefined,
    forwardTime: undefined,
    terminalTotal: undefined,
    ackTerminalTotal: undefined,
    levelTotal: undefined,
    earliestReceiveTime: undefined,
    latestReceivingTime: undefined,
    avgReceivingTime: undefined,
    ack1Id: undefined,
  }
  row.ack1Id = props.ack1Id
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>